<template>
    <div>
        <el-upload
                :action="uploadAction"
                list-type="picture-card"
                :file-list="picList"
                :on-success="handlePicSuccess"
                :on-remove="handlePicRemove"
                :on-preview="handlePictureCardPreview"
                :before-upload="beforeAvatarUpload"
        >
            <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog v-model="dialogVisible" size="full" :modal="false">
            <img width="100%" :src="dialogImageUrl" alt="">
        </el-dialog>
    </div>
</template>

<script>
    import util from '../../common/util'
    export default {
        props:{
            value: String,
            disabled:{
                type:Boolean,
                default:false
            }
        },
       data(){
           return {
               uploadAction:global.serverHost+"server/system/upload.aspx",
               picList:[],
               dialogImageUrl: '',
               dialogVisible: false
           }
        },
        watch:{
            value(val){
                this.valueChange(val)
            }
        },
        computed: {
            returnValue: function () {
                let result = "";
                this.picList.forEach(item=>{
                    if(result){
                        result+=";"
                    }
                    result += item.name
                })
                return result;
            }
        },
        methods:{
            handlePictureCardPreview(file) {
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            },
            handlePicSuccess(res, file){
                if(res && !res.success){
                    util.msg("上传出错")
                }else{
                    this.picList.push({name:res.filename,url: global.serverHost + "upload/" + res.filename})
                    this.$emit('input', this.returnValue)
                }
            },
            handlePicRemove(file, fileList){
                this.picList = fileList;
                this.$emit('input', this.returnValue)
            },
            enlargePic:function () {
                this.dialogVisible = true;
            },
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;

                if (!isJPG) {
                    this.$message.error('上传图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            },
            valueChange(val){
                if(val){
                    this.picList = []
                    val.split(";").forEach(item=>{
                        if(item) {
                            this.picList.push({name:item,url: global.serverHost + "upload/" + item})
                        }
                    })
                }
            }
        },
        created(){
            this.valueChange(this.value)
        }
    }
</script>
